package com.ming.sportdemo.controller;

import com.alibaba.fastjson.JSON;
import com.ming.sportdemo.bean.Clothes;
import com.ming.sportdemo.bean.QueryInfo;
import com.ming.sportdemo.common.Result;
import com.ming.sportdemo.config.ServerConfig;
import com.ming.sportdemo.service.ClothesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

@RestController
public class ClothesController {
    @Autowired
    private ClothesService clothesService;
    @Autowired
    private ServerConfig serverConfig;

    @RequestMapping("/allClothes")
    public String getClothesList(HttpServletRequest request,QueryInfo queryInfo){
        System.out.println(queryInfo.getPageNum());
        //获取最大列表数和当前编号
        System.out.println(""+queryInfo.getType());

        int numbers = clothesService.getClothesCounts("%"+queryInfo.getQuery()+"%");
        int pageStart = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();

        List<Clothes> clothes = clothesService.getAllClothes("%" + queryInfo.getType() + "%","%" + queryInfo.getQuery() + "%", pageStart, queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",clothes);
        String res_string = JSON.toJSONString(res);
        return res_string;
    }

    @RequestMapping("/allClothesName")
    public String allClothesName(){
        List<Clothes> clothes = clothesService.list();
        HashMap<String,Object> map = new HashMap<>();
        List<String> names = new LinkedList<>();
        for (int i = 0; i < clothes.size(); i++) {
            names.add(clothes.get(i).getClothesName());
        }
        map.put("data",names);
        String res = JSON.toJSONString(map);
        return res;
    }

    @RequestMapping("/addClothes")
    public String addClothes(@RequestBody Clothes clothes){
        String url = UploadController.baseURL;
        clothes.setClothesView(url);
        int i = clothesService.addClothes(clothes);
        return i>0?"success":"error";
    }
    @RequestMapping("/updateClothes")
    public String updateClothes(@RequestBody Clothes clothes){
        String url = UploadController.baseURL;
        clothes.setClothesView(url);
        String baseurl = serverConfig.getPort();
        if((baseurl+url).equals(clothes.getClothesView())){
            clothes.setClothesView(url);
        }
        int i = clothesService.updateClothes(clothes);
        System.out.println(i);
        System.out.println(clothes);
        return i>0?"success":"error";
    }
    @RequestMapping("/deleteClothes")
    public Result deleteClothes(int clothesId){
        int i = clothesService.deleteClothes(clothesId);
        String res = i>0?"success":"error";
        if(res.equals("success")){
            return Result.succ(res);
        }else {
            return Result.fail(res);
        }
    }
}
